package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.FollowBehaviorDto;
import com.heima.behavior.pojos.ApFollowBehavior;
import com.heima.behavior.service.ApFollowBehaviorService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.common.AppThreadLocal;
import org.bson.types.ObjectId;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApFollowBehaviorServiceImpl implements ApFollowBehaviorService {


    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public ResponseResult follow(FollowBehaviorDto dto) {
        //1.参数校验
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //1.5 获取登录人用户id
        ApUser apUser = AppThreadLocal.getApUser();

        if(apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //表示未登录
        if(apUser.getId()==0){
            //游客身份不能进行以下操作
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //2.用户是否已经关注
        //定义关注的key
        String followKey="USER_FOLLOW_"+apUser.getId();
        //定义粉丝的key
        String fanKey="USER_FAN_"+dto.getAuthorId();
        //判断hash中的大key中是的小key中是否包含，如果包含，则返回true,否则false
        Boolean hasKey = redisTemplate.opsForHash().hasKey(followKey, dto.getAuthorId() + "");
        //2.1 用户已经关注了，并且此时要执行关注操作 operation=0，直接返回
        if(hasKey&&dto.getOperation()==0){
            return ResponseResult.okResult("用户已经关注了");
        }
        //2.2 用户已经关注，则此时是取消关注操作,operation=1,删除redis和mongo
        if(hasKey&& dto.getOperation()==1){
            //1.删除redis中两个hash
            redisTemplate.opsForHash().delete(followKey, dto.getAuthorId()+"");
            redisTemplate.opsForHash().delete(fanKey, apUser.getId()+"");
            //2.删除mongo中的数据
            Query query = Query.query(Criteria.where("userId").is(apUser.getId().longValue()).and("followId").is(dto.getAuthorId()));
            mongoTemplate.remove(query,ApFollowBehavior.class);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        //2.3 用户没有关注，并且此时执行的是关注操作 operation=0,新增数据到redis和mongo中
        if(!hasKey && dto.getOperation()==0){
            //1.新增redis,两个hash
            redisTemplate.opsForHash().put(followKey,dto.getAuthorId()+"","1");
            redisTemplate.opsForHash().put(fanKey,apUser.getId()+"","1");
            //2.新增mongo数据
            ApFollowBehavior apFollowBehavior=new ApFollowBehavior();
            apFollowBehavior.setId(ObjectId.get().toHexString());
            apFollowBehavior.setFollowId(dto.getAuthorId());
            apFollowBehavior.setUserId(apUser.getId().longValue());
            apFollowBehavior.setArticleId(dto.getArticleId());
            apFollowBehavior.setCreatedTime(new Date());
            mongoTemplate.insert(apFollowBehavior);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.okResult("关注用户成功");
    }
}
